package com.ljfiction.book.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.ljfiction.book.Baner.Bookchapters;
import com.ljfiction.book.MyView.BorderTextView;
import com.ljfiction.book.R;
import com.ljfiction.book.activitys.Reading2Activity;

import com.ljfiction.book.tools.SettingManager;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Android on 2017/10/10.
 */

public class ReadReconntBookdatas extends BaseFragment {

    @BindView(R.id.expandable_readreconnt)
    ExpandableListView expandableReadreconnt;
    Unbinder unbinder;


    private Context context;
    private List<Bookchapters.DataBean.ChapterInfoBean> mContentChapterList = new ArrayList<>();
    private String bookId;
    private int scrollnum = 0;

    public ReadReconntBookdatas() {

    }

    public static ReadReconntBookdatas newInstance(Context context, List<Bookchapters.DataBean.ChapterInfoBean> mContentChapterList, String bookId) {
        ReadReconntBookdatas newFragment = new ReadReconntBookdatas();
        Bundle bundle = new Bundle();
        bundle.putSerializable("mContentChapterList", (Serializable) mContentChapterList);
        bundle.putString("booid", bookId);
        newFragment.setArguments(bundle);
        return newFragment;

    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        if (args != null) {
            this.mContentChapterList = (List<Bookchapters.DataBean.ChapterInfoBean>) args.getSerializable("mContentChapterList");
            this.context = getContext();
            this.bookId = args.getString("booid");

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.readreconntdatas_fragment, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        toscroll();
        return view;
    }

    private void initView() {
        expandableReadreconnt.setAdapter(new ReadReconntBookdatas.ContentAdpater());
        expandableReadreconnt.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                                        int groupPosition, long id) {
                return false;
            }
        });
        expandableReadreconnt.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                int cureentchapter = 0;
                for (int i = 0; i < groupPosition; i++) {
                    cureentchapter += mContentChapterList.get(i).getResultList().size();
                }
                SettingManager.getInstance().saveReadProgress(bookId, cureentchapter + childPosition, 0, 0, 1, 0);
                Intent intent = new Intent(context, Reading2Activity.class);
                intent.putExtra("bookId", bookId);
                startActivity(intent);
                return true;
            }
        });
        expandableReadreconnt.setOnScrollListener(null);
        expandableReadreconnt.setGroupIndicator(null);
    }

    public void toscroll() {
        scrollnum = 0;
        for (int i = 0; i < mContentChapterList.size(); i++) {
            expandableReadreconnt.expandGroup(i);
            if (mContentChapterList.get(i).getVolumeId() == SettingManager.getInstance().getBookVolumid(bookId)) {
                for (int j = 0; j < mContentChapterList.get(i).getResultList().size(); j++) {
                    if (mContentChapterList.get(i).getResultList().get(j).getChapterId() == SettingManager.getInstance().getBookChapterid(bookId)) {
                        scrollnum = scrollnum + i + j + 1;
                        expandableReadreconnt.smoothScrollToPosition(scrollnum);
                        break;
                    }
                }
            } else {
                scrollnum = scrollnum + mContentChapterList.get(i).getResultList().size();
            }
            scrollnum += mContentChapterList.get(i).getResultList().size();
        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    class ContentAdpater extends BaseExpandableListAdapter {


        @Override
        public int getGroupCount() {
            return mContentChapterList.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return mContentChapterList.get(groupPosition).getResultList().size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            return groupPosition;
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }


        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

            ReadReconntBookdatas.ContentAdpater.ParentViewHolder holder = null;
            if (convertView == null) {
                convertView = View.inflate(context, R.layout.content_classgroup_layout, null);
                holder = new ReadReconntBookdatas.ContentAdpater.ParentViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ReadReconntBookdatas.ContentAdpater.ParentViewHolder) convertView.getTag(); //取出ViewHolder对象
            }
            if (isExpanded) {
                //holder.contentAdapterGroupName.setTextColor(ContextCompat.getColor(context, R.color.pink));
                holder.contentAdapterImage.setImageResource(R.drawable.up);
            } else {
                //holder.contentAdapterGroupName.setTextColor(ContextCompat.getColor(context, R.color.zitiyanse));
                holder.contentAdapterImage.setImageResource(R.drawable.downcoment);
            }
            holder.contentAdapterGroupName.setText(mContentChapterList.get(groupPosition).getVolumeName());
            return convertView;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

            ReadReconntBookdatas.ContentAdpater.ChildViewHolder holder = null;
            if (convertView == null) {
                convertView = View.inflate(context, R.layout.content_chaptername_layout, null);
                holder = new ReadReconntBookdatas.ContentAdpater.ChildViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ReadReconntBookdatas.ContentAdpater.ChildViewHolder) convertView.getTag(); //取出ViewHolder对象
            }
            if ((mContentChapterList.get(groupPosition).getResultList().get(childPosition).getChapterId()) == SettingManager.getInstance().getBookChapterid(bookId)) {
                holder.bookchaptername.setTextColor(Color.parseColor("#f77583"));

            } else {
                holder.bookchaptername.setTextColor(Color.parseColor("#333333"));
            }
            holder.bookchaptername.setText(mContentChapterList.get(groupPosition).getResultList().get(childPosition).getChapterTitle());
            holder.comentsnum.setText(String.valueOf(mContentChapterList.get(groupPosition).getResultList().get(childPosition).getChapterLength()));
            if (mContentChapterList.get(groupPosition).getResultList().get(childPosition).getChapterIsvip() == 1) {
                holder.imageview.setVisibility(View.VISIBLE);
            } else {
                holder.imageview.setVisibility(View.INVISIBLE);
            }
            return convertView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

        class ParentViewHolder {
            @BindView(R.id.contentAdapter_groupName)
            TextView contentAdapterGroupName;
            @BindView(R.id.contentAdapter_image)
            ImageView contentAdapterImage;

            ParentViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }

        class ChildViewHolder {
            @BindView(R.id.bookchaptername)
            TextView bookchaptername;
            @BindView(R.id.comentsnum)
            BorderTextView comentsnum;
            @BindView(R.id.isVip)
            ImageView imageview;

            ChildViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        toscroll();
    }

    @Override
    public void onStart() {
        super.onStart();

    }

    @Override
    public void onPause() {
        super.onPause();
    }
}

